package edu.mse.camel.client.playback.handler;

import edu.mse.camel.client.core.events.model.BaselineResourceUpdate;
import edu.mse.camel.client.eventbus.pubsub.ISubscriber;
import edu.mse.camel.client.events.EventIDs;
import edu.mse.camel.client.events.IEvent;
import edu.mse.camel.client.events.peer.ReceiveGenericPeerEvent;
import edu.mse.camel.client.log.Debug;
import edu.mse.camel.client.playback.Activator;
import edu.mse.camel.client.playback.PlaybackManager;

public class ResourceUpdateHandler implements ISubscriber {
	
	public static final String POSTERBOARD_EXTN = "posterboard";
	
	@Override
	public void notifyEvent(IEvent event) {
		if(event instanceof ReceiveGenericPeerEvent) {
			ReceiveGenericPeerEvent generic = (ReceiveGenericPeerEvent) event;
			if ( generic.getEventID().equalsIgnoreCase(EventIDs.BASELINE_RESOURCE_UPDATE_EVENT)) {
				BaselineResourceUpdate update = generic.readObject(BaselineResourceUpdate.class);
				String projectName = update.getOriginalProjectName();
				String fileName = update.getOriginalFileName();
				if (fileName.endsWith(POSTERBOARD_EXTN)) {
					setPlaybackManagerProperties(projectName, fileName);
				}
			}
			else {
				Debug.log_warning(Activator.PLUGIN_ID, "WARNING: Unknown generic event type for ResourceUpdateHandler");
			}
		}
		else {
			Debug.log_warning(Activator.PLUGIN_ID, "WARNING: Unknown event type for ResourceUpdateHandler");
		}
	}

	private void setPlaybackManagerProperties(String projectName, String fileName) {
		PlaybackManager.INSTANCE.setPosterboardFile(fileName);
		PlaybackManager.INSTANCE.setProjectName(projectName);
		
	}

}
